//
//  UIView+Tool.m
//  UILbrary
//
//  Created by Apple on 2022/4/21.
//  Copyright © 2022 mac. All rights reserved.
//

#import "UIView+Tool.h"



@implementation UIView (Tool)


- (void)p_configCorderView_1
{
    UIView *view = [UIView new];
    view.frame = CGRectMake(10, 100, 300, 200);
    view.backgroundColor = [UIColor redColor];
//    [self.view addSubview:view];
    
    CGRect smallRect = CGRectMake(-50, -50, 100, 100);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:smallRect cornerRadius:50] bezierPathByReversingPath]];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    
    view.layer.mask = shapeLayer;
}

- (void)addReverseCorner:(UIViewCornerType)type radius:(CGFloat )radius
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];

    if (type & UIViewCornerTypeLeftTop) {
        [path appendPath:[self circlePath:UIViewCornerTypeLeftTop radius:radius]];
    }
    if (type & UIViewCornerTypeLeftBottom) {
        [path appendPath:[self circlePath:UIViewCornerTypeLeftBottom radius:radius]];
    }
    if (type & UIViewCornerTypeRightTop) {
        [path appendPath:[self circlePath:UIViewCornerTypeRightTop radius:radius]];
    }
    if (type & UIViewCornerTypeRightBottom) {
        [path appendPath:[self circlePath:UIViewCornerTypeRightBottom radius:radius]];
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    
    self.layer.mask = shapeLayer;

}

- (void)addReverseCornerRadius:(CGFloat )radius
{
    [self addReverseCorner:UIViewCornerTypeLeftTop|UIViewCornerTypeLeftBottom|UIViewCornerTypeRightTop|UIViewCornerTypeRightBottom radius:radius];
    return;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];

    [path appendPath:[self circlePath:UIViewCornerTypeLeftTop radius:radius]];
    [path appendPath:[self circlePath:UIViewCornerTypeLeftBottom radius:radius]];
    [path appendPath:[self circlePath:UIViewCornerTypeRightTop radius:radius]];
    [path appendPath:[self circlePath:UIViewCornerTypeRightBottom radius:radius]];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    
    self.layer.mask = shapeLayer;
}


- (UIBezierPath *)circlePath:(UIViewCornerType)type radius:(CGFloat )radius
{
    CGFloat x = 0;
    CGFloat y = 0;

    switch (type) {
        case UIViewCornerTypeLeftTop:
            x = -radius;
            y = -radius;
            break;
        case UIViewCornerTypeLeftBottom:
            //radius +
            x = -radius;
            y = self.bounds.size.height - radius;
            break;
            
        case UIViewCornerTypeRightTop:
            //radius +
            x = self.bounds.size.width - radius;
            y = -radius;
            break;
            
        case UIViewCornerTypeRightBottom:
            //radius +
            x = self.bounds.size.width - radius;
            y = self.bounds.size.height - radius;
            break;
            
        default:
            break;
    }
    
    CGRect rect = CGRectMake(x, y, radius * 2, radius * 2);

    UIBezierPath *circlePath = [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] bezierPathByReversingPath];
    return circlePath;
}

@end
